/*
 * Menu.java
 * Main function menu allow users to access the different activity.
 * This page also tap on the open space to display Admob Ads for Ad revenue generation
 * 1 - News.java
 * 2 - Start Camera intent
 * 3 - BrowseUpload.java
 * 
 */
package edu.ntu.mobilestomp;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Menu extends Activity implements OnClickListener {

	private Uri imageUri;
	private ImageButton newsButton;
	private ImageButton browseButton;
	private ImageButton cameraButton;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);

		// Create the adView
	    AdView adView = new AdView(this, AdSize.BANNER, "a14da14ce498a17");
	    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
	    // Add the adView to it
	    layout.addView(adView);
	    AdRequest request = new AdRequest();

	    request.setTesting(true);
	    // Initiate a generic request to load it with an ad
	    adView.loadAd(request);
	    
	    // Assigning resources to ImageButton
	    newsButton = (ImageButton) findViewById(R.id.news);
	    browseButton = (ImageButton) findViewById(R.id.browse);
	    cameraButton = (ImageButton) findViewById(R.id.camera);

	    //Set OnClickListener properties to ImageButton
	    newsButton.setOnClickListener(this);
	    browseButton.setOnClickListener(this);
	    cameraButton.setOnClickListener(this);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    switch (requestCode) {
	    case 0:
	        if (resultCode == Activity.RESULT_OK) {
	            Uri selectedImage = imageUri;
	            String selectedImagePath = selectedImage.getPath();

				Intent myIntent = new Intent(getApplicationContext(), BrowseUpload.class);
				myIntent.putExtra("pic_path", selectedImagePath);

				startActivityForResult(myIntent, 1);
				
	        }
	    }
	}

    // ImageButton onClick
	public void onClick(View v) {
		// Using resource ID to identify the button clicked

		// Set intent to News.java
		if (v.getId()== R.id.news){
			Intent myIntent = new Intent(v.getContext(), News.class);
			startActivityForResult(myIntent, 0);
		}
		// Set intent to launch Camera function
		else if (v.getId()== R.id.camera){
		    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		    // Set file name of the taken picture
		    File photo = new File(Environment.getExternalStorageDirectory()+"/MobileStomp",  "MS_"+System.currentTimeMillis()+".jpg");
		    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
		    imageUri = Uri.fromFile(photo);
		    startActivityForResult(intent, 0);
		}
		// Set intent to BrowseUpload.java
		else if (v.getId()== R.id.browse){
			Intent myIntent = new Intent(v.getContext(), BrowseUpload.class);
			startActivityForResult(myIntent, 0);
		}
		// Catch for abnormal activity
		else{
			Toast.makeText(getApplicationContext(), R.string.error_msg, 5).show();
		}
		
	}
}
